<?php
declare (strict_types = 1);

namespace app\common\exception;

use think\exception\Handle;
use think\Response;
use Throwable;

#[class('/common/exception/Http', '异常处理')]
class Http extends Handle
{
    protected $http_status = 500;

    public function render($request, Throwable $e): Response
    {
        $message = $e->getMessage();
        $code    = $e->getCode();
        if (config('http.internal_error') !== 0) {
            $message = '服务器内部错误';
        }
        $code = config('http.api_http_code.error');

        // 返回状态码
        if ($e instanceof \think\Exception) {
            return result(null, $code, $message);
        }

        if (method_exists($e, "getStatusCode")) {
            $http_status = $e->getStatusCode();
        } else {
            $http_status = $this->http_status;
        }
        return result(null, $code, $message, $http_status);
    }
}
